package com.example.homework_5_2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.room.Room;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    EditText account, password;
    Button login_btn;

    RoodDataBase roodDataBase;
    int id = 0;

    private class Check_Account_Thread extends Thread {
        @Override
        public void run() {
            int status = check_has_this_account();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (status == 0) { //new account
                        Data data = new Data("", "", "", password.getText().toString(), account.getText().toString(), false);
                        roodDataBase.dataDao().insertDataMore(data);
                        Toast.makeText(MainActivity.this, "已为你创建账号，请添加个人资料。", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MainActivity.this, Create_Account_Page.class);
                        intent.putExtra("account_text", account.getText().toString());
                        intent.putExtra("password_text", password.getText().toString());
                        intent.putExtra("id", data.getId());
                        startActivity(intent);
                        finish();
                    }
                    else if (status == 1) {
                        Toast.makeText(MainActivity.this, "已成功登入。", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MainActivity.this, First_Page.class);
                        intent.putExtra("account", account.getText().toString());
                        intent.putExtra("password", password.getText().toString());
                        startActivity(intent);
                        finish();
                    }
                    else if (status == 2) {
                        Toast.makeText(MainActivity.this, "个人资料未上传完毕。", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MainActivity.this, Create_Account_Page.class);
                        intent.putExtra("account_text", account.getText().toString());
                        intent.putExtra("password_text", password.getText().toString());
                        intent.putExtra("id", id);
                        startActivity(intent);
                        finish();
                    }
                    else if (status == 3) {
                        Toast.makeText(MainActivity.this, "密码输入错误。", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        roodDataBase = Room.databaseBuilder(this, RoodDataBase.class, "dataDB").allowMainThreadQueries().build();

        account = findViewById(R.id.account);
        password = findViewById(R.id.password);
        login_btn = findViewById(R.id.login_btn);

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (account.getText().toString().isEmpty() || password.getText().toString().isEmpty()) {
                    Toast.makeText(MainActivity.this, "Please enter account and password", Toast.LENGTH_SHORT).show();
                }
                else {
                    Check_Account_Thread checkAccountThread = new Check_Account_Thread();
                    checkAccountThread.start();
                }
            }
        });
    }

    public int check_has_this_account() {
        try {
            List<Data> data_list = roodDataBase.dataDao().get_data();
            if (!data_list.isEmpty()) {
                for (int i = 0; i < data_list.size(); i++) {
                    Data data = data_list.get(i);
                    if (account.getText().toString().equals(data.getAccount_name()) && !password.getText().toString().equals(data.getAccount_password())) {
                        return 3;
                    }
                    else if (account.getText().toString().equals(data.getAccount_name()) && password.getText().toString().equals(data.getAccount_password())) {
                        if (data.isIs_upload_data()) {
                            return 1;
                        }
                        else {
                            id = data.getId();
                            return 2;
                        }
                    }
                }
            }
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}